﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YJKArchMethodLibrary.Utils;
using YJKElementFinish.ColumnSurface.Controllers;
using YJKElementFinish.ColumnSurface.DataDefinition;
using YJKElementFinish.ColumnSurface.Managers;

namespace YJKElementFinish.ColumnSurface.Forms
{
	public partial class SurfaceLayerSettingForm : System.Windows.Forms.Form
	{
		public SurfaceLayerSettingForm(Document doc, SurfaceLayerSettingForm.FormStyle formStyle, List<WallTypeLayerInfos> alternativeWallTypeLayerInfo)
		{
			this.m_Doc = doc;
			this.m_FormStyle = formStyle;
			this.m_AlternativeWallTypeLayerInfo = alternativeWallTypeLayerInfo;
			this.InitializeComponent();
		}

		public List<WallTypeLayerInfos> CurrentWallTypeLayerInfo
		{
			get
			{
				return this.m_CurrentWallTypeLayerInfo;
			}
		}

		public SurfaceLayerSettingForm.FormResultStyle ResultStyle
		{
			get
			{
				return this.m_FormResultStyle;
			}
		}

		private void SurfaceLayerEditForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			if (this.m_FormStyle == SurfaceLayerSettingForm.FormStyle.SLT_ExternalWall)
			{
				this.Text = "外墙饰面";
				this.btnSearch.Visible = true;
				this.btnEdit.Visible = true;
			}
			else if (this.m_FormStyle == SurfaceLayerSettingForm.FormStyle.SLT_InternalWall)
			{
				this.Text = "内墙饰面";
				this.btnSearch.Visible = true;
				this.btnEdit.Visible = true;
			}
			else if (this.m_FormStyle == SurfaceLayerSettingForm.FormStyle.SLT_Column)
			{
				this.Text = "柱子饰面";
				this.btnSearch.Visible = false;
				this.btnEdit.Left = this.btnSearch.Left;
				this.btnEdit.Visible = true;
			}
			else
			{
				this.Text = "添加饰面";
				this.btnSearch.Visible = false;
				this.btnEdit.Visible = false;
			}
			this.UpdateWallTypeCombobox();
		}

		private void UpdateWallTypeCombobox()
		{
			this.cmbboxWallType.Items.Clear();
			foreach (WallTypeLayerInfos wallTypeLayerInfos in this.m_AlternativeWallTypeLayerInfo)
			{
				this.cmbboxWallType.Items.Add(wallTypeLayerInfos.TypeName);
			}
			this.cmbboxWallType.Visible = false;
		}

		private void SurfaceLayerEditForm_FormClosing(object sender, FormClosingEventArgs e)
		{
            if (this.ResultStyle == SurfaceLayerSettingForm.FormResultStyle.FRS_Edit && ((System.Windows.Forms.Form)sender).DialogResult == DialogResult.Cancel)
			{
				this.m_FormResultStyle = SurfaceLayerSettingForm.FormResultStyle.FRS_OK;
				e.Cancel = true;
			}
            if (((System.Windows.Forms.Form)sender).DialogResult != DialogResult.OK)
			{
				return;
			}
			if (this.m_FormResultStyle != SurfaceLayerSettingForm.FormResultStyle.FRS_OK)
			{
				return;
			}
			if (this.CurrentWallTypeLayerInfo.Count == 0)
			{
				MessageBox.Show("未添加贴面墙类型", "盈建科提示");
				e.Cancel = true;
			}
			if (AddSurfaceLayersController.GetRefCurvesCount(this.m_Doc) == 0)
			{
				string text = "未添加贴面参考线 ，点击‘编辑’添加参考线";
				if (this.m_FormStyle == SurfaceLayerSettingForm.FormStyle.SLT_InternalWall || this.m_FormStyle == SurfaceLayerSettingForm.FormStyle.SLT_ExternalWall)
				{
					text = "未添加贴面参考线 ，点击‘搜索’或‘编辑’添加参考线";
				}
				MessageBox.Show(text, "盈建科提示");
				e.Cancel = true;
			}
			foreach (WallTypeLayerInfos wallTypeLayerInfos in this.m_CurrentWallTypeLayerInfo)
			{
				wallTypeLayerInfos.Update(this.m_Doc);
			}
		}

		private void btnAddLayer_Click(object sender, EventArgs e)
		{
			if (this.m_AlternativeWallTypeLayerInfo.Count > 0)
			{
				WallTypeLayerInfos item2 = this.m_AlternativeWallTypeLayerInfo[0].DeepClone();
				this.m_CurrentWallTypeLayerInfo.Add(item2);
				this.UpdateCurrentTypeInfoGridView(this.m_CurrentWallTypeLayerInfo.Count - 1);
				this.cmbboxWallType.Visible = false;
				return;
			}
			base.Enabled = false;
			List<WallLayerInfos> wallTypeLayerInfos = this.CreateWallLayerInfosList();
			CompoundStructureSettingForm compoundStructureSettingForm = new CompoundStructureSettingForm(this.m_Doc, wallTypeLayerInfos);
			if (compoundStructureSettingForm.ShowDialog() != DialogResult.OK)
			{
				base.Enabled = true;
				return;
			}
			base.Enabled = true;
			List<WallLayerInfos> wallLayerInfo = compoundStructureSettingForm.WallLayerInfo;
			List<WallLayerInfos> list = new List<WallLayerInfos>();
			int i = 0;
			int num = -1;
			int exteriorKernelLayerIndex = -1;
			while (i < wallLayerInfo.Count)
			{
				list.Add(wallLayerInfo[i]);
				if (wallLayerInfo[i].StructureLayer == null)
				{
					if (num == -1)
					{
						num = list.Count;
					}
					else
					{
						exteriorKernelLayerIndex = list.Count - 1;
					}
				}
				i++;
			}
			string text = WallTypeManager.GetTypeNameByLayers(wallLayerInfo);
			text = this.GetValidName(text);
			WallTypeLayerInfos newStructLayerInfo = new WallTypeLayerInfos(text, num, exteriorKernelLayerIndex, list);
			int num2 = this.m_AlternativeWallTypeLayerInfo.FindIndex((WallTypeLayerInfos item) => item.TypeName.CompareTo(newStructLayerInfo.TypeName) == 0);
			if (num2 != -1)
			{
				newStructLayerInfo = this.m_AlternativeWallTypeLayerInfo[num2];
			}
			else
			{
				this.m_AlternativeWallTypeLayerInfo.Add(newStructLayerInfo);
				this.UpdateWallTypeCombobox();
			}
			this.m_CurrentWallTypeLayerInfo.Add(newStructLayerInfo);
			this.UpdateCurrentTypeInfoGridView(this.m_CurrentWallTypeLayerInfo.Count - 1);
			this.cmbboxWallType.Visible = false;
		}

		private string GetValidName(string name)
		{
			bool flag = false;
			List<int> list = new List<int>();
			foreach (object obj in this.cmbboxWallType.Items)
			{
				string text = obj.ToString();
				if (text.IndexOf(name) != -1)
				{
					int num = text.LastIndexOf("(");
					if (num == -1)
					{
						continue;
					}
					string value = text.Substring(num + 1, text.Length - num - 2);
					try
					{
						int item2 = Convert.ToInt32(value);
						list.Add(item2);
					}
					catch (Exception)
					{
						continue;
					}
				}
				if (text.CompareTo(name) == 0)
				{
					flag = true;
				}
			}
			if (list.Count != 0)
			{
				int i = 1;
				int num2 = list[list.Count - 1] + 1;
				while (i < num2)
				{
					int num3 = list.FindIndex((int item) => item == i);
					if (num3 == -1)
					{
						return name + "(" + i.ToString() + ")";
					}
					i++;
				}
				return name + "(" + i.ToString() + ")";
			}
			if (flag)
			{
				return name + "(1)";
			}
			return name;
		}

		private List<WallLayerInfos> CreateWallLayerInfosList()
		{
			List<WallLayerInfos> list = new List<WallLayerInfos>();
			double val = 20.0;
			double num = Common.MMToFeet(val);
            MaterialFunctionAssignment materialFunctionAssignment = (MaterialFunctionAssignment)4;
			ElementId invalidElementId = ElementId.InvalidElementId;
			CompoundStructureLayer structureLayer = new CompoundStructureLayer(num, materialFunctionAssignment, invalidElementId);
			string layerNameByMaterialFunction = WallTypeManager.GetLayerNameByMaterialFunction(materialFunctionAssignment);
			WallLayerInfos item = new WallLayerInfos(structureLayer, layerNameByMaterialFunction, "按类别", val.ToString());
			list.Add(item);
			return list;
		}

		private void btnDeleteLayer_Click(object sender, EventArgs e)
		{
			if (this.dbgTypeInfoList.SelectedCells.Count != 1)
			{
				return;
			}
			int rowIndex = this.dbgTypeInfoList.SelectedCells[0].RowIndex;
			this.m_CurrentWallTypeLayerInfo.RemoveAt(rowIndex);
			this.UpdateCurrentTypeInfoGridView(rowIndex);
			this.cmbboxWallType.Visible = false;
		}

		private void btnMoveDown_Click(object sender, EventArgs e)
		{
			if (this.dbgTypeInfoList.SelectedCells.Count != 1)
			{
				return;
			}
			int rowIndex = this.dbgTypeInfoList.SelectedCells[0].RowIndex;
			if (rowIndex == this.dbgTypeInfoList.RowCount - 1)
			{
				return;
			}
			this.m_CurrentWallTypeLayerInfo.Insert(rowIndex + 2, this.m_CurrentWallTypeLayerInfo[rowIndex]);
			this.m_CurrentWallTypeLayerInfo.RemoveAt(rowIndex);
			this.UpdateCurrentTypeInfoGridView(rowIndex + 1);
			this.cmbboxWallType.Visible = false;
		}

		private void btnMoveUp_Click(object sender, EventArgs e)
		{
			if (this.dbgTypeInfoList.SelectedCells.Count != 1)
			{
				return;
			}
			int rowIndex = this.dbgTypeInfoList.SelectedCells[0].RowIndex;
			if (rowIndex == 0)
			{
				return;
			}
			this.m_CurrentWallTypeLayerInfo.Insert(rowIndex - 1, this.m_CurrentWallTypeLayerInfo[rowIndex]);
			this.m_CurrentWallTypeLayerInfo.RemoveAt(rowIndex + 1);
			this.UpdateCurrentTypeInfoGridView(rowIndex - 1);
			this.cmbboxWallType.Visible = false;
		}

		private void UpdateCurrentTypeInfoGridView(int rowIndex)
		{
			this.dbgTypeInfoList.Rows.Clear();
			int num = 1;
			foreach (WallTypeLayerInfos wallTypeLayerInfos in this.m_CurrentWallTypeLayerInfo)
			{
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				DataGridViewTextBoxCell dataGridViewTextBoxCell = new DataGridViewTextBoxCell();
				dataGridViewTextBoxCell.Value = num.ToString();
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell);
				DataGridViewTextBoxCell dataGridViewTextBoxCell2 = new DataGridViewTextBoxCell();
				dataGridViewTextBoxCell2.Value = wallTypeLayerInfos.TypeName;
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell2);
				DataGridViewButtonCell dataGridViewButtonCell = new DataGridViewButtonCell();
				dataGridViewButtonCell.Value = "...";
				dataGridViewRow.Cells.Add(dataGridViewButtonCell);
				this.dbgTypeInfoList.Rows.Add(dataGridViewRow);
				num++;
			}
			if (rowIndex > -1 && rowIndex < this.dbgTypeInfoList.RowCount)
			{
				this.dbgTypeInfoList.Rows[rowIndex].Cells[0].Selected = true;
			}
		}

		private void cmbboxWallType_SelectedIndexChanged(object sender, EventArgs e)
		{
			int rowIndex = this.dbgTypeInfoList.CurrentCell.RowIndex;
			WallTypeLayerInfos wallTypeLayerInfos = this.m_CurrentWallTypeLayerInfo[rowIndex];
			string newValue = this.cmbboxWallType.Items[this.cmbboxWallType.SelectedIndex].ToString();
			if (newValue.CompareTo(wallTypeLayerInfos.TypeName) == 0)
			{
				this.cmbboxWallType.Visible = false;
				return;
			}
			this.cmbboxWallType.Visible = false;
			int num = this.m_AlternativeWallTypeLayerInfo.FindIndex((WallTypeLayerInfos item) => item.TypeName.CompareTo(newValue) == 0);
			if (num != -1)
			{
				this.m_CurrentWallTypeLayerInfo[rowIndex] = this.m_AlternativeWallTypeLayerInfo[num];
			}
			this.UpdateCurrentTypeInfoGridView(rowIndex);
		}

		private void dbgTypeInfoList_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			this.ClickTypeColumn(sender, e);
			this.ClickEditColumn(sender, e);
		}

		private void ClickEditColumn(object sender, DataGridViewCellEventArgs e)
		{
			if (e.ColumnIndex != 2 || e.RowIndex == -1)
			{
				return;
			}
			try
			{
				this.cmbboxWallType.Visible = false;
				CompoundStructureSettingForm compoundStructureSettingForm = new CompoundStructureSettingForm(this.m_Doc, this.m_CurrentWallTypeLayerInfo[e.RowIndex].LayerList);
				if (compoundStructureSettingForm.ShowDialog() == DialogResult.OK)
				{
					List<WallLayerInfos> wallLayerInfo = compoundStructureSettingForm.WallLayerInfo;
					this.m_CurrentWallTypeLayerInfo[e.RowIndex].LayerList = compoundStructureSettingForm.WallLayerInfo;
					string newName = WallTypeManager.GetTypeNameByLayers(this.m_CurrentWallTypeLayerInfo[e.RowIndex].LayerList);
					this.m_CurrentWallTypeLayerInfo[e.RowIndex].TypeName = newName;
					int num = this.m_AlternativeWallTypeLayerInfo.FindIndex((WallTypeLayerInfos item) => item.TypeName.CompareTo(newName) == 0);
					if (num == -1)
					{
						this.m_AlternativeWallTypeLayerInfo.Add(this.m_CurrentWallTypeLayerInfo[e.RowIndex]);
					}
					this.UpdateWallTypeCombobox();
					this.UpdateCurrentTypeInfoGridView(e.RowIndex);
				}
			}
			catch (Exception ex)
			{
				string message = ex.Message;
			}
		}

		private void ClickTypeColumn(object sender, DataGridViewCellEventArgs e)
		{
			if (e.ColumnIndex != 1 || e.RowIndex == -1)
			{
				return;
			}
			DataGridViewCell dataGridViewCell = this.dbgTypeInfoList.Rows[e.RowIndex].Cells[e.ColumnIndex];
			dataGridViewCell.Value.ToString();
			int x = this.dbgTypeInfoList.Location.X;
			int y = this.dbgTypeInfoList.Location.Y;
			Rectangle cellDisplayRectangle = this.dbgTypeInfoList.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
			int x2 = cellDisplayRectangle.X;
			int y2 = cellDisplayRectangle.Y;
			int x3 = x + x2;
			int y3 = y + y2;
            this.cmbboxWallType.Location = new System.Drawing.Point(x3, y3);
			this.cmbboxWallType.Size = cellDisplayRectangle.Size;
			int selectedIndex = this.cmbboxWallType.FindString(dataGridViewCell.Value.ToString());
			this.cmbboxWallType.SelectedIndex = selectedIndex;
			this.cmbboxWallType.Visible = true;
			this.cmbboxWallType.BringToFront();
		}

		private void toolStripMenuItemRename_Click(object sender, EventArgs e)
		{
			if (this.dbgTypeInfoList.CurrentCell == null)
			{
				return;
			}
			int rowIndex = this.dbgTypeInfoList.CurrentCell.RowIndex;
			if (rowIndex == -1)
			{
				return;
			}
			WallTypeLayerInfos wallTypeLayerInfos = this.m_CurrentWallTypeLayerInfo[rowIndex];
			string oldName = wallTypeLayerInfos.TypeName;
			RenameForm renameForm = new RenameForm(wallTypeLayerInfos.TypeName);
			if (renameForm.ShowDialog() == DialogResult.OK)
			{
				string validName = this.GetValidName(renameForm.NewName);
				this.m_CurrentWallTypeLayerInfo[rowIndex].TypeName = validName;
				int num = this.m_AlternativeWallTypeLayerInfo.FindIndex((WallTypeLayerInfos item) => item.TypeName.CompareTo(oldName) == 0);
				if (num != -1)
				{
					this.m_AlternativeWallTypeLayerInfo[num].TypeName = validName;
				}
				this.dbgTypeInfoList.Rows[rowIndex].Cells[1].Value = this.m_CurrentWallTypeLayerInfo[rowIndex].TypeName;
				this.cmbboxWallType.Visible = false;
				this.UpdateWallTypeCombobox();
			}
		}

		private void btnSearch_Click(object sender, EventArgs e)
		{
			this.m_FormResultStyle = SurfaceLayerSettingForm.FormResultStyle.FRS_Search;
		}

		private void btnEdit_Click(object sender, EventArgs e)
		{
			this.m_FormResultStyle = SurfaceLayerSettingForm.FormResultStyle.FRS_Edit;
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			this.m_FormResultStyle = SurfaceLayerSettingForm.FormResultStyle.FRS_OK;
		}

		private Document m_Doc;

		private SurfaceLayerSettingForm.FormStyle m_FormStyle;

		private SurfaceLayerSettingForm.FormResultStyle m_FormResultStyle = SurfaceLayerSettingForm.FormResultStyle.FRS_OK;

		private List<WallTypeLayerInfos> m_AlternativeWallTypeLayerInfo = new List<WallTypeLayerInfos>();

		private List<WallTypeLayerInfos> m_CurrentWallTypeLayerInfo = new List<WallTypeLayerInfos>();

		public enum FormStyle
		{
			SLT_ExternalWall,
			SLT_InternalWall,
			SLT_Column,
			SLT_Other
		}

		public enum FormResultStyle
		{
			FRS_OK = 1,
			FRS_Search,
			FRS_Edit
		}
	}
}
